package com.hang.gymmanager.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hang.gymmanager.Enum.CommonConstant;
import com.hang.gymmanager.Enum.CommonEnum;
import com.hang.gymmanager.entity.UserInfoEntity;
import com.hang.gymmanager.entity.WalletEntity;
import com.hang.gymmanager.exception.BusinessException;
import com.hang.gymmanager.mapper.UserInfoMapper;
import com.hang.gymmanager.mapper.WalletMapper;
import com.hang.gymmanager.req.GetUserInfoPageReq;
import com.hang.gymmanager.req.UpdatePasswordReq;
import com.hang.gymmanager.req.changeUserStatusReq;
import com.hang.gymmanager.res.UserInfoDetailRes;
import com.hang.gymmanager.res.UserInfoPageRes;
import com.hang.gymmanager.service.UserInfoService;
import com.hang.gymmanager.util.PasswordEncodeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoEntity> implements UserInfoService {

    private final WalletMapper walletMapper;

    /**
     * 获取用户分页查询信息
     * @param req
     * @return
     */
    @Override
    public Page<UserInfoPageRes> getAllUser(GetUserInfoPageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<UserInfoPageRes> res = getBaseMapper().getAllUser(req,page);
        return res;
    }

    /**
     * 查询单个用户详情
     * @param userId
     * @return
     */
    @Override
    public UserInfoDetailRes getUserDetail(Integer userId) {
        QueryWrapper<UserInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserInfoEntity::getUserId,userId);
        UserInfoEntity entity = this.getBaseMapper().selectOne(wrapper);
        if(ObjectUtil.isNull(entity)){
            throw new BusinessException("查询不到该用户，请检查userId");
        }
        UserInfoDetailRes res = new UserInfoDetailRes();
        BeanUtils.copyProperties(entity,res);
        WalletEntity walletEntity = walletMapper.selectById(userId);
        res.setBalance(walletEntity==null?null:walletEntity.getBalance());
        return res;
    }

    @Override
    public void changeUserStatus(changeUserStatusReq req) {
        QueryWrapper<UserInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserInfoEntity::getUserId,req.getUserId());
        UserInfoEntity entity = this.getBaseMapper().selectOne(wrapper);
        if(ObjectUtil.isNull(entity)){
            throw new BusinessException("查询不到该用户，请检查userId");
        }
        if(CommonEnum.YES.getValue().equals(entity.getRecFlag())){
            entity.setRecFlag(CommonEnum.NO.getValue());
        }else {
            entity.setRecFlag(CommonEnum.YES.getValue());
        }
        int i = this.getBaseMapper().updateById(entity);
        if(i<1){
            throw new BusinessException("更改用户状态失败");
        }
    }

    @Override
    public void resetPassword(Integer userId) {
        String encode = PasswordEncodeUtil.passwordEncode(CommonConstant.NORMAL_PASSWORD,CommonConstant.PASSWORD_SALT);
        UserInfoEntity entity = new UserInfoEntity();
        entity.setUserId(userId);
        entity.setPassword(encode);
        int i = this.getBaseMapper().updateById(entity);
        if(i!=1){
            throw new BusinessException("用户不存在，重新确定用户id");
        }
    }
}
